import Mock from 'mockjs'

// 生成模拟数据
const generateList = () => {
  return Mock.mock({
    'list|10': [{
      'id|+1': 1,
      'name': '@ctitle(5, 10)',
      'description': '@cparagraph(1, 2)',
      'thumbnail': Mock.Random.image('400x300', '#4A7BF7','DataV'),
      'createTime': '@datetime("yyyy-MM-dd HH:mm:ss")',
      'updateTime': '@datetime("yyyy-MM-dd HH:mm:ss")',
      'status|1': ['published', 'draft'],
      'creator': '@cname',
      // 添加页面配置和组件数据
      pageConfig: {
        title: '@ctitle(5, 10)',
        backgroundColor: '#0d2a42',
        width: 1920,
        height: 1080,
        backgroundImage: ''
      },
      components: []
    }]
  }).list
}

const screenList = generateList()

export default [
  {
    url: '/api/screen/list',
    method: 'get',
    response: () => ({
      code: 200,
      data: screenList,
      message: 'success'
    })
  },
  {
    url: '/api/screen/detail/:id',
    method: 'get',
    response: ({ url }) => {
      const id = parseInt(url.match(/\/(\d+)$/)[1])
      const screen = screenList.find(item => item.id === id)
      if (screen) {
        return {
          code: 200,
          data: {
            ...screen,
            pageConfig: {
              title: screen.name,
              backgroundColor: '#0d2a42',
              width: 1920,
              height: 1080,
              backgroundImage: ''
            },
            components: []
          },
          message: 'success'
        }
      }
      return {
        code: 404,
        message: '大屏不存在'
      }
    }
  },
  {
    url: '/api/screen/add',
    method: 'post',
    response: ({ body }) => {
      const data = typeof body === 'string' ? JSON.parse(body) : body
      const newScreen = {
        id: Mock.mock('@integer(100, 999)'),
        ...data,
        createTime: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
        updateTime: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
        status: 'draft',
        creator: Mock.mock('@cname')
      }
      screenList.unshift(newScreen)
      return {
        code: 200,
        data: newScreen,
        message: '创建成功'
      }
    }
  },
  {
    url: '/api/screen/update/:id',
    method: 'put',
    response: ({ body }) => {
      const data = typeof body === 'string' ? JSON.parse(body) : body
      const index = screenList.findIndex(item => item.id === data.id)
      if (index > -1) {
        screenList[index] = {
          ...screenList[index],
          ...data,
          updateTime: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")')
        }
        return {
          code: 200,
          data: screenList[index],
          message: '更新成功'
        }
      }
      return {
        code: 404,
        message: '大屏不存在'
      }
    }
  },
  {
    url: '/api/screen/delete/:id',
    method: 'delete',
    response: ({ url }) => {
      const id = parseInt(url.match(/\/(\d+)$/)[1])
      const index = screenList.findIndex(item => item.id === id)
      if (index > -1) {
        screenList.splice(index, 1)
        return {
          code: 200,
          message: '删除成功'
        }
      }
      return {
        code: 404,
        message: '大屏不存在'
      }
    }
  }
] 